﻿#pragma once
#include<assert.h>
#include<iostream>//c++的输入输出流
using std::cout, std::endl;
namespace myspace
{
	/*list节点的模版函数*/
	template<class T>
	struct __list_node
	{
		/*成员变量*/
		__list_node<T>* _next;
		__list_node<T>* _prev;
		T _data;
		/*构造*/__list_node(T x=T())
			:_next(nullptr),_prev(nullptr),_data(x)
		{}
	};

	/*list迭代器的模版函数*/
	template<class T,class Ref,class Ptr>
	struct __list_iterator
	{
		typedef __list_node<T> Node;
		/*成员变量*/
		Node* _node;

		/*构造*/__list_iterator(Node* node)
			:_node(node){ }

		/*解引用符号重载*/T& operator*()
		{
			return _node->_data;
		}
		/*->符号重载*/T& operator->()
		{
			return &_node->_data;
		}
		/*++符号重载*/__list_iterator<T, T&, T*>& operator++()
		{
			_node=_node->_next;
			return *this;
		}
		/*--符号重载*/__list_iterator<T, T&, T*>& operator--()
		{
			_node = _node->_prev;
			return *this;
		}
		/*++符号重载(后置)*/__list_iterator<T, T&, T*> operator++(int)
		{
			__list_iterator<T, T&, T*> tmp(*this);
			_node = _node->_next;
			return tmp;
		}
		/*--符号重载(后置)*/__list_iterator<T, T&, T*> operator--(int)
		{
			__list_iterator<T, T&, T*> tmp(*this);
			_node = _node->_prev;
			return tmp;
		}
		/*!=符号重载*/bool operator!=(const __list_iterator<T, T&, T*>& it)
		{
			return _node != it._node;
		}
		/*==符号重载*/bool operator==(const __list_iterator<T, T&, T*>& it)
		{
			return _node == it._node;
		}
	};

	/*模拟list的模版函数*/
	template<class T>//模版类的函数声明和定义要放在一起
	class list
	{
		typedef __list_node<T> Node;

	public:
		/*普通迭代器*/typedef  __list_iterator<T,T&,T*> iterator;
		iterator begin()
		{
			return iterator(_head->_next);
		}
		iterator end()
		{
			return iterator(_head);
		}
		/*const迭代器*/typedef  __list_iterator<T, const T&, const T*> const_iterator;
		iterator cbegin()
		{
			return iterator(_head->_next);
		}
		iterator cend()
		{
			return iterator(_head);
		}
		/*构造*/list()
		{
			_head = new Node;
			_head->_next = _head;
			_head->_prev = _head;
		}
		/*构造*/
		/*拷贝构造*/list(const list<T>& lt)
		{

		}
		/*析构*/~list()
		{
			clear();
			delete _head;
			_head = nullptr;
		}
		void clear()
		{
			iterator it = begin();
			while (it != end())
			{
				erase(it++);
			}
		}
		/*赋值*/list<T>& operator=(const list<T>& lt)
		{

		}
		/*const赋值*/
		/*尾插*/void push_back(const T& x)
		{
			Node* tail = _head->_prev;
			Node* newnode = new Node(x);

			tail->_next = newnode;
			newnode->_prev = tail;
			newnode->_next = _head;
			_head->_prev = newnode;
		}
		/*尾删*/void pop_back();
		/*头插*/void push_front(const T& x);
		/*头删*/void pop_front();
		/*指定位置插入*/void insert(iterator pos,const T& x);
		/*指定位置删除*/iterator erase(iterator pos)
		{
			pos._node->_prev->_next = pos._node->_next;
			pos._node->_next->_prev = pos._node->_prev;
			iterator tmp = pos._node->_prev;
			delete pos._node;
			pos= tmp;
			return pos;
		}



	private:
		Node* _head;
	};
	void print_list(list<int>& l)
	{
		list<int>::iterator it = l.begin();
		while (it != l.end())
		{
			cout << *it << " ";
			++it;
		}
		cout << endl;
	}
	void test_list1()
	{
		cout << "test_list1" << endl;
		list<int> l1;
		
		l1.push_back(1);
		l1.push_back(2);
		l1.push_back(3);
		l1.push_back(4);
		print_list(l1);
		list<int>::iterator it = l1.begin();
		it=l1.erase(++it);
		
		while (it != l1.end())
		{
			cout << *it << " ";
			++it;
		}
		cout << endl;

		cout << "test_list1" << endl;
	}
	void test_list2()
	{
		cout << "test_list2" << endl;

		cout << "test_list2" << endl;
	}
	
}